use std::env; //提供与执行环境相关的函数和类型
use std::str::FromStr;
fn main() {
    let mut numbers = Vec::new(); //可增长向量类型
    for arg in env::args().skip(1) {
        numbers.push(u64::from_str(&arg).expect("error parse argument"))
    }

    if numbers.len() == 0 {
        eprintln!("Usage: gcm NUMBER ...");
        std::process::exit(1);
    }

    let mut d = numbers[0];
    for m in &numbers[1..] {
        d = gcm2(d, *m);
    }
    let mut d2 = numbers[0];
    //& 借用符 * 解引用符
    for m in &numbers[1..] {
        d2 = gcm(d2, *m);
    }
    println!("The greatest common divisor of {:?} is {}", numbers, d);
    println!("The greatest common divisor of {:?} is {}", numbers, d2);
}
fn gcm(n: u64, m: u64) -> u64 {
    if m == 0 && n == 0 {
        0
    } else {
        if m == 0 { n } else { gcm(m, n % m) }
    }
}

fn gcm2(mut n: u64, mut m: u64) -> u64 {
    //宏调用
    assert!(n != 0 && m != 0);
    while m != 0 {
        let temp = m;
        m = n % m;
        n = temp;
    }
    n
}

#[test] //这里标记为这是一个测试函数 cargo test 会运行目录下的所有测试函数
fn test_gcm() {
    assert_eq!(gcm(10, 5), 5);
    assert_eq!(gcm2(20, 10), 10)
}
